package org.seanye.erp.view.model;

import java.beans.PropertyChangeSupport;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class WeekStore extends PropertyChangeSupport implements Serializable {

    private static final long serialVersionUID = -1540206405508820943L;

    public static final String P_BALANCE = "balance";
    public static final String P_WARN = "warnNumber";
    public static final String P_PRIVOUS = "privousWeekTotal";

    public WeekStore() {
	super("");
	startDate = Calendar.getInstance().getTime();
    }

    public WeekStore(Object sourceBean) {
	super(sourceBean);
	startDate = Calendar.getInstance().getTime();
    }

    private Date startDate;
    private Double prviousWeekTotal = 0.0;
    private Double balance = 0.0;
    private Double warnNumber = 50.0;
    private Double input = 0.0;
    private List<StoreItem> weekItems = new ArrayList<StoreItem>(7) {
	private static final long serialVersionUID = 5211116726573097672L;

	{
	    Date time = Calendar.getInstance().getTime();
	    add(new StoreItem(time));
	    add(new StoreItem(time));
	    add(new StoreItem(time));
	    add(new StoreItem(time));
	    add(new StoreItem(time));
	    add(new StoreItem(time));
	    add(new StoreItem(time));
	}
    };

    public Double getInput() {
	return input;
    }

    public void setInput(Double input) {
	Double old = this.input;
	this.input = input;
	firePropertyChange("input", old, this.input);
    }

    public Double getTotalStock() {
	return getPrviousWeekTotal() + getInput();
    }

    public Date getStartDate() {
	return startDate;
    }

    public void setStartDate(Date startDate) {
	this.startDate = startDate;
    }

    public Double getPrviousWeekTotal() {
	return prviousWeekTotal;
    }

    public void setPrviousWeekTotal(Double prviousWeekTotal) {
	this.prviousWeekTotal = prviousWeekTotal;
	this.firePropertyChange(P_PRIVOUS, this.getPrviousWeekTotal(),
		prviousWeekTotal);
    }

    public List<StoreItem> getWeekItems() {
	return weekItems;
    }

    public void setWeekItems(List<StoreItem> weekItems) {
	this.weekItems = weekItems;
    }

    public Double totalShipment() {
	double amount = 0;
	for (StoreItem item : weekItems) {
	    amount += item.getShipment();
	}
	return amount;
    }

    public Double getBalance() {
	double balance = getTotalStock() - totalShipment();
	double oldBalance = this.balance;
	this.balance = balance;
	if (oldBalance != balance) {
	    this.firePropertyChange(P_BALANCE, oldBalance, balance);
	}
	return this.balance;
    }

    public Double getWarnNumber() {
	return warnNumber;
    }

    public void setWarnNumber(Double warnNumber) {
	Double old = this.warnNumber;
	this.warnNumber = warnNumber;
	this.firePropertyChange(P_WARN, old, warnNumber);
    }

}
